<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Goods_img short summary.
 *
 * Goods_img description.
 *
 * @version 1.0
 * @author GALAXY
 */

class Goods_img_model extends CI_Model
{
    public $goods_img_id;
    public $goods_id;
    public $goods_color_id;
    public $imgurl;
    public $thumbnail_img;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function insert_goods_img($goods_imgModel)
    {
        return $this->db->insert('goods_img',$goods_imgModel);
    }

    //由颜色id 获取颜色
    public function get_goods_img_by_id($goods_img_id)
    {
        return $this->db->select()->from('goods_img')->where(array('goods_img_id'=>$goods_img_id))->get()->row();
    }

    //获取所有颜色
    public function get_allgoods_img()
    {
        return $this->db->select()->from('goods_img')->get()->result();
    }

    //有商品获取商品图片
    public function get_goods_img_by_goodsid($goods_id)
    {
        return $this->db->select()->from('goods_img')->where(array('goods_id'=>$goods_id))->get()->result();
    }



    //更新颜色
    public function edit_goods_img($goods_imgModel)
    {

        return  $this->db->update('goods_img',$goods_imgModel,array('goods_img_id'=>$goods_imgModel->goods_img_id));

    }

    //删除颜色
    public function del_goods_img_byid($goods_img_id)
    {
        return $this->db->delete('goods_img',array('goods_img_id'=>$goods_img_id));
    }

    //批量删除颜色
    public function batchdel_goods_img($goods_img_ids)
    {
        $this->db->trans_begin();
        foreach($goods_img_ids as $goods_imgiditem)
        {
            $this->db->delete('goods_img',array('goods_img_id'=>$goods_imgiditem));
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }



    /**
     * @param mixed $cpage
     * @param mixed $pagesize
     * @param mixed $files
     * @param mixed $tabel
     * @param mixed $order
     */
    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where,$temStr)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where $order limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id $temStr";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }

}